/*
 * @Author: Yuluo 568055454@qq.com
 * @Date: 2023年06月18日 06:44:22
 * @LastEditors: Yuluo 568055454@qq.com
 * @LastEditTime: 2023年06月18日 14:25:48
 * @FilePath: \Mood-Blog-System\back\models\Comment.js
 * @Description:  Comment表模型定义文件
 * Copyright (c) 2023 by Yuluo, All Rights Reserved. 
 */

const { mongoose } = require('../db');

const schema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        autopopulate: true,
        required: true,
        index: true,
    },
    page: String,

    content: {
        type: String,
        required: true,
        trim: true,
    },
    parent: {
        type: mongoose.Schema.Types.ObjectId,
        default: null,
        ref: 'Comment',
        autopopulate: true,
    },
    children: {
        type: Array,
        default: [],
        ref: 'Comment',
        autopopulate: true,
    },
    status: { type: String, default: 'normal', enum: ['normal', 'disabled', 'deleted'] },
    deletedAt: { type: Date, default: null },
}, {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
});

schema.virtual('likeCount', {
    ref: 'Like',
    localField: '_id',
    foreignField: 'comment',
    count: true
});

schema.plugin(require('mongoose-autopopulate'));
module.exports = mongoose.model('Comment', schema, 'comment');